package com.qfedu.yanglao20admin.inform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.CommonResult;
import com.qfedu.yanglao20.common.annotation.LogInfo;
import com.qfedu.yanglao20.entity.inform.AlarmDispose;
import com.qfedu.yanglao20admin.inform.param.AlarmDisposeParam;
import com.qfedu.yanglao20admin.inform.param.AlarmRecordParam;
import com.qfedu.yanglao20admin.inform.service.AlarmDisposeService;
import com.qfedu.yanglao20admin.inform.service.AlarmRecordService;
import com.qfedu.yanglao20admin.inform.vo.AlarmRecordVO;
import com.qfedu.yanglao20admin.inform.vo.CountAlarmRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2023/4/28 10:19
 */
@Api(tags = "告警纪录")
@RestController
@RequestMapping("/inform")
public class AlarmRecordController {

    @Resource
    private AlarmRecordService alarmRecordService;

    @Resource
    private AlarmDisposeService alarmDisposeService;

    @LogInfo
    @ApiOperation(value = "分页告警纪录信息", notes = "分页告警纪录信息")
    @GetMapping("/page")
    public CommonResult<AlarmRecordVO> page(AlarmRecordParam param) {
        Page<AlarmRecordVO> alarmRecordVOPage = alarmRecordService.pageAlarmRecord(param);
        return CommonResult.success(alarmRecordVOPage);
    }

    @LogInfo
    @ApiOperation(value = "处理纪录以及修改状态", notes = "处理纪录以及修改状态")
    @GetMapping("/handle")
    public CommonResult<AlarmDispose> handleDispose(AlarmDisposeParam param) {
        alarmDisposeService.addAlarmDispos(param);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "告警纪录统计", notes = "告警纪录统计")
    @GetMapping("/count")
    public CommonResult<CountAlarmRecordVO> countAlarmRecord(@ApiParam("告警记录数组参数") String[] timeList) {
        String[] array = new String[timeList.length];

        for (int i = 0; i < timeList.length; i++) {
            if (timeList[i].length() > 10) {
                int index = timeList[i].indexOf(" ");
                String substring = timeList[i].substring(0, index);
                array[i] = substring;
            } else {
                array[i] = timeList[i];
            }

        }

        List<CountAlarmRecordVO> countAlarmRecordVOS = alarmRecordService.countAlarmTime(array);
        return CommonResult.success(countAlarmRecordVOS);
    }
}
